//
//  ToolUtil.m
//  River
//
//  Created by iMacOne on 15/11/27.
//  Copyright © 2015年 iMacOne. All rights reserved.
//

#import "ToolUtil.h"
#import "Header.h"
#import "ImageUtil.h"
#import "SecondControl.h"
#import "AppDelegate.h"
@implementation ToolUtil
+ (void)creatAlertControl:(id)viewControl alertString:(NSString *)str
{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction * enterAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
//        [NSThread sleepForTimeInterval:];
        [viewControl dismissViewControllerAnimated:YES completion:nil];
        
    }];
    [alertController addAction:enterAction];
    [viewControl presentViewController:alertController animated:YES completion:nil];
    
}

+(NSData*)returnDataWithDictionary:(NSDictionary*)dict
{
    NSMutableData* data = [[NSMutableData alloc]init];
    NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:dict forKey:@"talkData"];
    [archiver finishEncoding];
    
    return data;
}
#pragma mark 获得字符串的字节长度
+ (int)getStrLen:(NSString*)text {
    if ([ToolUtil isBlank:text]) {
        return 0;
    }
    int length = 0;
    for (int i = 0; i < text.length; i++) {
        int ascii = [[text substringWithRange:NSMakeRange(i, 1)] characterAtIndex:0];
        if (ascii >= 0 && ascii <= 255) {
            length++;
        } else {
            length += 2;
        }
    }
    return length;
}

#pragma mark 判断字符串是否为空
+ (BOOL) isBlank:(NSString *)string {
    if (string == nil || string == NULL) {
        return YES;
    }
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (string.length == 0) {
        return YES;
    }
    return NO;
}
+ (void)callPhone:(UIViewController *)control phone:(NSString*)phone
{
    [ToolUtil showOther:control type:@"tel" value:phone];

}
+ (void)showOther:(UIViewController*)control type:(NSString*)type value:(NSString*)value {
    if ([ToolUtil isBlank:type] || [ToolUtil isBlank:value]) {
        return;
    }
    
    UIWebView *callWebview = [[UIWebView alloc] init];
    callWebview.backgroundColor = [UIColor yellowColor];
    NSString *telUrl = [NSString stringWithFormat:@"%@:%@", type, value];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:telUrl]]];
    [control.view addSubview:callWebview];
    
}

+ (BOOL)isPhoneNumber:(NSString *)mobileNum
{
    
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    return [regextestmobile evaluateWithObject:mobileNum];
    
}
+ (float)createLine:(float)y superView:(UIView*)view lift:(float)lift width:(float)width hight:(float)hight color:(UIColor *)color
{
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(lift, y, width, hight)];
    line.backgroundColor = color;
    [view addSubview:line];
    return CGRectGetMaxY(line.frame);
}
+ (float)createVerticalLine:(float)y superView:(UIView*)view lift:(float)lift height:(float)height
{
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(lift, y, 0.5, height)];
    line.backgroundColor = [ImageUtil colorWithRgb:0xeeeeee];
    [view addSubview:line];
    return CGRectGetMaxY(line.frame);
    
}
//添加下划线
+ (NSMutableAttributedString *)addHyperLine:(NSString *)content
{
    NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:content];
    NSRange range = {0,[str length]};
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
    return str;
}

//自适应高度
+ (CGFloat)customAutoHeigh:(NSString *)contentString width:(CGFloat)width num:(CGFloat)num
{
    CGRect rect = [contentString boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT(num)} context:nil];
    return rect.size.height;
}
//自适应宽度
+ (CGFloat)autoWidth:(NSString *)nameString width:(CGFloat)width height:(CGFloat)height num:(CGFloat)num
{
    CGSize size = CGSizeMake(width, height);
    //    CGSize labelSize = [nameString sizeWithFont:FONT(num) constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
    CGRect  labelSize = [nameString boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT(num)} context:nil];
    return labelSize.size.width;
    
}
+ (CGSize)autoWidth1:(NSString *)nameString width:(CGFloat)width height:(CGFloat)height num:(CGFloat)num
{
    CGSize size = CGSizeMake(width, height);
    //    CGSize labelSize = [nameString sizeWithFont:FONT(num) constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
    CGRect  labelSize = [nameString boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT(num)} context:nil];
    return labelSize.size;
}
+ (NSMutableAttributedString *)labelHeight:(CGFloat)height string:(NSString *)str{
 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

 [paragraphStyle setLineSpacing:height];//调整行间距

 [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];

    return attributedString;
}
+ (NSMutableAttributedString *)labelAttributeDict:(NSDictionary *)dic
{
    NSString * labelText = dic[@"labelText"];
    NSString * begin = dic[@"begin"];
    NSString * end = dic[@"end"];
    NSString * textFont = dic[@"textFont"];
    NSString * name = dic[@"Name"];
    NSArray * values = dic[@"values"];
//    NSString * string = NSForegroundColorAttributeName;
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:labelText];
//    NSInteger i = labelText.length;
    [str addAttribute:name value:values[0] range:NSMakeRange(0, str.length)];
    [str addAttribute:name value:values[1] range:NSMakeRange([begin integerValue], [end integerValue])];

//    [str addAttribute:NSForegroundColorAttributeName value:HOME_COLOR range:NSMakeRange([begin integerValue], [end integerValue])];
//
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:[textFont integerValue]] range:NSMakeRange([begin integerValue], [end integerValue])];
//
//    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:181/255. green:181/255. blue:181/255. alpha:1.0] range:NSMakeRange(i-2, 2)];
    return str;
   
}
#pragma makr - 提示框
+ (void)blackColorLabel:(NSString *)str{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(WindowWidth/5, (WindowHeight-80)/2, WindowWidth/5*3, 80)];
    view.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
    view.layer.masksToBounds = YES;
    //    backLabel.alpha = 0.8;
    [view.layer setShadowOffset:CGSizeMake(0, 0)]; // 阴影的范围
    view.layer.cornerRadius = 5.0;
    [view.layer setShadowRadius:6]; // 阴影扩散的范围控制
    [view.layer setShadowOpacity:1]; // 阴影透明度
    [view.layer setShadowColor:[UIColor colorWithRed:0.7321 green:0.7285 blue:0.7356 alpha:1.0].CGColor]; // 阴影的颜色
    view.layer.shadowPath =  [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
    UILabel*   backLabel = [[UILabel alloc] initWithFrame:CGRectMake(WindowWidth/20, 0, WindowWidth/2, 80)];
    [view addSubview:backLabel];
    backLabel.text = str;
    backLabel.numberOfLines = 0;
    backLabel.textColor = [UIColor whiteColor];
    backLabel.font = FONT(14);
    
    
    
    //    backLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
    backLabel.textAlignment = NSTextAlignmentCenter;
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate.window addSubview:view];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [backLabel removeFromSuperview];
        [view removeFromSuperview];
    });
}


+ (BOOL)isValidateEmail:(NSString *)email

{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
    
    return [emailTest evaluateWithObject:email];
    
}
@end
